package service.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dao.CommentDao;
import dao.ReplyDao;
import entity.Reply;
import service.ReplyService;

@Service
public class ReplyServiceImpl implements ReplyService {

	@Autowired
	SqlSessionFactory factory;
	
	@Override
	public int add(Reply reply) {
		SqlSession sqlSession=null;
		try {
			sqlSession=factory.openSession();
			ReplyDao dao = sqlSession.getMapper(ReplyDao.class);
			int count = dao.add(reply);
			sqlSession.commit();
			return count;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			sqlSession.close();
		}
		return 0;
	}

	@Override
	public int updateReplyState(int type, int replyId) {
		SqlSession sqlSession=null;
		try {
			sqlSession=factory.openSession();
			ReplyDao dao = sqlSession.getMapper(ReplyDao.class);
			int count = dao.updateReplyState(type, replyId);
			sqlSession.commit();
			return count;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			sqlSession.close();
		}
		return 0;
	}

	@Override
	public List<Reply> findByState(int stateId) {
		SqlSession sqlSession=null;
		try {
			sqlSession=factory.openSession();
			ReplyDao dao = sqlSession.getMapper(ReplyDao.class);
			List<Reply> list = dao.findByState(stateId);
			return list;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			sqlSession.close();
		}
		return null;
	}

}
